package com.baaio.conn;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

import com.baaio.conn.Interface.BaaioConn;
import com.baaio.reflect.BaaioReflect;
/**
 * 事务处理的实现类。主要用于对数据的删，增，改，查。<br>
 * 是BaaioConnImpl的复制。<br>
 * 
 * 但是事务处理必须要使用这个类来处理数据，否则事务处理会失败！<br>
 * @author Administrator
 *
 */
public class TransactionConnImpl implements BaaioConn
{
	private static TransactionConnImpl baaio;
	private static TransactionConnImpl baaio1;
	private ArrayList<String> list;
	private GetSqlString gss;
	private BaaioConnImpl bcis;
	
	private TransactionConnImpl(String DBName1)
	{
		gss = new GetSqlString();
		bcis = new BaaioConnImpl(DBName1);
		
		list = new ArrayList<String>();
	}
	
	private TransactionConnImpl()
	{
		list = new ArrayList<String>();
		gss = new GetSqlString();
		bcis =new BaaioConnImpl();
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 获得该类的实例。
	 * @return
	 */
	protected static TransactionConnImpl getBaaioInstance()
	{
		if(baaio == null)
		{
			baaio = new TransactionConnImpl();
			
		}
		return baaio;
	}
	
	/**
	 * 获得该类的实例。
	 * @param DBName
	 * @return
	 */
	protected static TransactionConnImpl getBaaioInstance(String DBName)
	{
		if(baaio1 == null)
		{
			baaio1 = new TransactionConnImpl(DBName);
		}
		return baaio1;
	}

	@Override
	public boolean saveData(Object object)
	{
		BaaioReflect br = new BaaioReflect();
		return list.add(gss.dbInsert(br.getTableName(object),br.getValue(object)));
	}

	public ResultSet queryData(String tableName, ArrayList<String> list, String condiction)
	{
		return bcis.queryData(tableName,list,condiction);
		
	}

	public ResultSet queryData(String tableName, String fields, String condiction)
	{
		return bcis.queryData(tableName,fields,condiction);
	}

	public boolean saveData(String tableName, HashMap<String, String> map)
	{
		return list.add(gss.dbInsert(tableName,map));
	}

	public boolean removeData(String tableName, String condition)
	{
		return list.add(gss.dbDelete(tableName,condition));
	}

	public boolean removeData(String tableName, String cName,
			ArrayList<String> list3)
	{
		ArrayList<String> list2 = gss.dbDelete(tableName,cName,list3);
		boolean f = false;
		for(String sql : list2)
		{
			f = list.add(sql);
		}
		return f;
	}

	public boolean removeData(String tableName, String cName, String condiction)
	{
		return list.add(gss.dbDelete(tableName,cName,condiction));

	}

	public boolean updateData(String tableName, HashMap<String, String> map,
			String upCondition)
	{

		return list.add(gss.dbUpdate(tableName,map,upCondition));
	}
	
	
	@Override
	public boolean updateData(Object object, Object upConditionObject)
	{

		BaaioReflect br = new BaaioReflect();
		return list.add(gss.dbUpdate(br.getTableName(object), br.getValue(object), br.getUpConditionString(upConditionObject)));
		
		
	}

	/**
	 * 接收两个object对象<br>
	 * 第一个是更新的值对象<br>
	 * 第二个是要更新目标对象<br>
	 * 
	 * @param object
	 * @param upObject
	 */
	public void updataData(Object object,Object upObject)
	{
		BaaioReflect br = new BaaioReflect();
		list.add(gss.dbUpdate(br.getTableName(object),br.getValue(object),br.getUpConditionString(upObject)));
	}

	public ResultSet query(String sql)
	{
		return bcis.query(sql);
	}

	protected ArrayList<String> getList()
	{

		return list;
	}
	
	protected void clear()
	{
		list.clear();
	}

	public void close()
	{
		bcis.close();
	}

	@Override
	public boolean removeData(Object object, String value)
	{

		 BaaioReflect brf = new BaaioReflect();
			String key =brf.getKeyValue(object).get("key");
			String tableName = brf.getTableName(object);
			return list.add("delete * from "+tableName+ " where " + key +"='" + value + "'");
		
	}

	@Override
	public ResultSet queryX(String tableName)
	{

		
		return bcis.queryX(tableName);
	}

	@Override
	public ResultSet queryData(Object object, String value)
	{

		return bcis.queryData(object,value);
	}

	@Override
	public ResultSet lift(Object A, Object B)
	{

		return bcis.lift(A,B);
	}

	@Override
	public boolean hasData(String tabelName, String item, String value)
	{

		return bcis.hasData(tabelName,item,value);
	}

	@Override
	public boolean clearTableTruncate(String tableName) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Object queryDataB(Object object, String value) {
		// TODO Auto-generated method stub
		return bcis.queryDataB(object, value);
	}

	@Override
	public Object queryDataA(Object object) {
		// TODO Auto-generated method stub
		return bcis.queryDataA(object);
	}

	@Override
	public boolean hasData(Object object) {
		// TODO Auto-generated method stub
		return bcis.hasData(object);
	}

}
